﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DcsGauges.Gui.Gauges.Interfaces;
using DcsGauges.Shared.DataStructures;
using DcsGauges.Gui.Rendering;
using DcsGauges.DataStructures;
using DcsGauges.Gui.Gauges.ValueConverters;
using DcsGauges.Shared.Exceptions;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using DcsGauges.Gui.Gauges.Generic;

namespace DcsGauges.Gui.Gauges.Panels
{
    class OxygenPanel : GuiRenderableBase, IMultiValueGauge
    {
        #region Fields & Properties
        
        private Texture2D                       backgroundTexture                       = null;
        private TriLeverGauge                   oxygenStateLever                        = null;
        private TwoLeverGauge                   oxygenRegulatorLever                    = null;
        private TwoLeverGauge                   oxygenSupplyLever                       = null;
        private ToggleStateGauge                flow                                    = null;
        private CircularGauge                   pressure                                = null;

        #endregion


        #region Constructors

        public OxygenPanel(GuiRenderableBase parent, URect area) : base(parent, area)
        {
            this.backgroundTexture              = TextureManager.GetTexture("Textures/Panels/Oxygen");

            this.oxygenStateLever               = new TriLeverGauge(this, new URect(new ULoc(0.187f, 0.64f), new USize(0.045f, 0.3f)), 
                null,
                DcsProperties.Panels.Right.Oxygen.EmergencyLever,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/S3LeverState0"),
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/S3LeverState1"),
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/S3LeverState2"),
                new TriLeverStateValueConverter());
            
            this.oxygenRegulatorLever           = new TwoLeverGauge(this, new URect(new ULoc(0.48f, 0.64f), new USize(0.045f, 0.3f)),
                DcsProperties.Panels.Right.Oxygen.Regulator,
                null,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/S3LeverState0"),
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/S3LeverState2"),
                new TwoLeverStateValueConverter());

            this.oxygenSupplyLever              = new TwoLeverGauge(this, new URect(new ULoc(0.78f, 0.64f), new USize(0.045f, 0.3f)),
                DcsProperties.Panels.Right.Oxygen.Supply,
                null,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/S3LeverState0"),
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/S3LeverState2"),
                new TwoLeverStateValueConverter());

            this.flow                           = new ToggleStateGauge(this, new URect(new ULoc(0.2f, 0.35f), new USize(0.1f, 0.1f)), 
                DcsProperties.Panels.Right.Oxygen.FlowIndicator, 
                TextureManager.GetTexture("Textures/Panels/Foregrounds/FlowInactive"), 
                TextureManager.GetTexture("Textures/Panels/Foregrounds/FlowActive"),
                new BoolConverter(0.5f));
            
            this.pressure                       = new CircularGauge(this, new URect(new ULoc(0.59f, 0.19f), new USize(0.3f, 0.3f)), DcsProperties.Panels.Right.Oxygen.Pressure, 
                new LinearConverter(new InOutMapping(0.0f, 4.712f), new InOutMapping(1.0f, 7.853f)), 
                null, 
                TextureManager.GetTexture("Textures/Gauges/Needles/EngineGaugesNeedle"));
        }

        #endregion


        #region Interface Implemenation

        #region Properties

        public DcsProperty[] Properties
        {
            get 
            { 
                return new DcsProperty [] 
                { 
                    this.oxygenRegulatorLever.DataProperty, 
                    this.oxygenRegulatorLever.DataProperty,
                    this.oxygenSupplyLever.DataProperty,
                    this.flow.DataProperty,
                    this.pressure.DataProperty,
                };
            }
        }

        public void SetValue(Shared.DataStructures.DcsProperty property, float value)
        {
            if      (property == this.oxygenRegulatorLever.DataProperty)    this.oxygenRegulatorLever.Value     = value;
            else if (property == this.oxygenStateLever.DataProperty)        this.oxygenStateLever.Value         = value;
            else if (property == this.oxygenSupplyLever.DataProperty)       this.oxygenSupplyLever.Value        = value;
            else if (property == this.flow.DataProperty)                    this.flow.Value                     = value;
            else if (property == this.pressure.DataProperty)                this.pressure.Value                 = value;
            else throw new DcsArgumentException(string.Format(Localization.ExcPropertyInvalidForGauge2A, property, typeof(OxygenPanel)));
        }

        public float GetValue(Shared.DataStructures.DcsProperty property)
        {
            if      (property == this.oxygenSupplyLever.DataProperty)       return this.oxygenSupplyLever.Value;
            else if (property == this.oxygenStateLever.DataProperty)        return this.oxygenStateLever.Value;
            else if (property == this.oxygenSupplyLever.DataProperty)       return this.oxygenSupplyLever.Value;
            else if (property == this.flow.DataProperty)                    return this.flow.Value;
            else if (property == this.pressure.DataProperty)                return this.pressure.Value;
            else throw new DcsArgumentException(string.Format(Localization.ExcPropertyInvalidForGauge2A, property, typeof(OxygenPanel)));
        }

        #endregion

        #endregion


        #region Overrides

        public override void Render(SpriteBatch spriteBatch, GraphicsDevice device, Rectangle parentRect, float globalZIndex)
        {
            Rectangle? originalDim                  = this.keepAspectRatio ? this.backgroundTexture.Bounds : new Rectangle?();
            Rectangle viewportRect                  = this.CalcViewportRect(parentRect, originalDim);

            spriteBatch.Draw(this.backgroundTexture, viewportRect, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, base.NextFrontIndex(globalZIndex, 1));
            
            this.oxygenRegulatorLever.Render(spriteBatch, device, viewportRect, base.NextFrontIndex(globalZIndex, 2));
            this.oxygenStateLever.Render(spriteBatch, device, viewportRect, base.NextFrontIndex(globalZIndex, 2));
            this.oxygenSupplyLever.Render(spriteBatch, device, viewportRect, base.NextFrontIndex(globalZIndex, 2));
            this.flow.Render(spriteBatch, device, viewportRect, base.NextFrontIndex(globalZIndex, 2));
            this.pressure.Render(spriteBatch, device, viewportRect, base.NextFrontIndex(globalZIndex, 2));
        }

        #endregion
    }
}
